package com.zaiteng;

import java.io.File;
import java.io.FileNotFoundException;

import org.json.JSONException;
import org.json.JSONObject;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.objects.collections.List;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

@ShortName("JMessage")
@Author("Icefairy333")
@Version(1.20f)
@Permissions(values={"android.permission.RECEIVE_USER_PRESENT","android.permission.INTERNET","android.permission.WAKE_LOCK","android.permission.READ_PHONE_STATE","android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE","android.permission.VIBRATE","android.permission.MOUNT_UNMOUNT_FILESYSTEMS","android.permission.ACCESS_NETWORK_STATE","android.permission.SYSTEM_ALERT_WINDOW","android.permission.WRITE_SETTINGS"})
@DependsOn(values={"jmessage-android-1.2.3"})
@Events(values={"gotResult(responseCode as Int,responseMessage as String,JsonStr as String)"})
public class JMsgWrapper {
	/**
	 * jmessage技术文档
	 * http://docs.jpush.io/guideline/jmessage_android_guide/
	 */
	private BasicCallback mBC;
	private BA mba;
	private String en;
	public void init(BA ba,String en1,boolean debug){
		mba=ba;
		this.en=en1.toLowerCase(BA.cul);
		JMessageClient.init(BA.applicationContext);
		JMessageClient.setDebugMode(debug);
		mBC=new BasicCallback() {
			
			@Override
			public void gotResult(int responseCode, String responseMessage) {
				// TODO Auto-generated method stub
				mba.raiseEvent(this, en+"_gotResult".toLowerCase(), new Object[]{responseCode,responseMessage,""});
			}
		};
	}
	/**
	 * extVal=null
	 * @param username
	 * @param password
	 */
	public void register(String username, String password){
		JMessageClient.register(username, password, mBC);
	}
	/**
	 * extVal=null
	 * @param username
	 * @param password
	 */
	public void login(String username, String password){
		JMessageClient.login(username, password, mBC);
	}
	/**
	 * extVal=null
	 * @param username
	 * @param password
	 */
	public void logout(){
		JMessageClient.logout();
	}
	/**
	 * got the result in callback event
	 * @param username
	 */
	public void getUserInfo(String username){
		JMessageClient.getUserInfo(username, new GetUserInfoCallback() {
			
			@Override
			public void gotResult(int responseCode, String responseMessage, UserInfo ui) {
				// TODO Auto-generated method stub
				JSONObject ob=new JSONObject();
				try {
					ob.put("ui", ui);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mba.raiseEvent(this, en+"_gotResult".toLowerCase(), new Object[]{responseCode,responseMessage,ob.toString()});
			}
		});
	}
	/**
	 * json string
	 */
	public String getMyInfo(){
		UserInfo ui= JMessageClient.getMyInfo();
		JSONObject jo=new JSONObject();
		try {
			jo.put("ui", ui);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jo.toString();
	}
	public void updateUserPassword(String oldPassword, String newPassword){
		JMessageClient.updateUserPassword(oldPassword, newPassword, mBC);
	}
	public void updateUserAvatar(String avatarPath){
		JMessageClient.updateUserAvatar(new File(avatarPath), mBC);
	}
	/**
	 * 
	 * @param username to send
	 * @param text to send
	 */
	public MessageWrapper createSingleTextMessage(String username, String text){
		MessageWrapper mw=new MessageWrapper();
		mw.Initialize(JMessageClient.createSingleTextMessage(username, text));
		return mw;
	}
	public MessageWrapper createGroupTextMessage(long groupID, String text){
		MessageWrapper mw=new MessageWrapper();
		mw.Initialize(JMessageClient.createGroupTextMessage(groupID, text));
		return mw;
	}
	/**
	 * if file not useable will return null
	 * @param username
	 * @param imageFile
	 */
	public MessageWrapper createSingleImageMessage(String username, String imageFile) {
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createSingleImageMessage(username, new File(imageFile)));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	public MessageWrapper createGroupImageMessage(long groupID, String imageFile) {
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createGroupImageMessage(groupID, new File(imageFile)));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	public MessageWrapper createSingleVoiceMessage(String username, 
			  String voiceFile, int duration){
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createSingleVoiceMessage(username, new File(voiceFile), duration));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	public MessageWrapper createGroupVoiceMessage(long groupID, 
			  String voiceFile, int duration){
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createGroupVoiceMessage(groupID, new File(voiceFile), duration));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	/**
	 * send the message object
	 * @param msg
	 */
	public void sendMessage(MessageWrapper msg) {
		JMessageClient.sendMessage(msg.getMessage());
	}
	/**
	 * 
	 * @return List<Conversation_im>
	 */
	public List getConversationList(){
		java.util.List<Conversation> lst=JMessageClient.getConversationList();
		List ret=new List();
		ret.Initialize();
		for(int i=0;i<lst.size();i++){
			ConversationWrapper cw=new ConversationWrapper();
			cw.Initialize(lst.get(i));
			ret.Add(cw);
		}
		return ret;
	}
	public ConversationWrapper getSingleConversation(String username){
		ConversationWrapper cw=new ConversationWrapper();
		cw.Initialize(JMessageClient.getSingleConversation(username));
		return cw;
	}
	public ConversationWrapper getGroupConversation(long GroupId){
		ConversationWrapper cw=new ConversationWrapper();
		cw.Initialize(JMessageClient.getGroupConversation(GroupId));
		return cw;
	}
	public void deleteSingleConversation(String username){
		JMessageClient.deleteSingleConversation(username);
	}
}
